﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;
namespace WX.WMS.BLL
{
    public class StoreHouseBLL:BaseBLL
    {
        private BaseDAO DAO = null;
        public StoreHouseBLL()
        {
            DAO = new BaseDAO();
        }
        public IEnumerable<T_STOCK> GetStockList()
        {
            return DAO.T_STOCKs.OrderBy(t=>t.STOCK_NAME).ToList();
        }

        public T_STOCK GetStockByID(string ID)
        {
            return DAO.T_STOCKs.Where(t => t.STOCK_ID.ToString() == ID).SingleOrDefault();
        }
        public int AddStock(T_STOCK Stock)
        {
            try
            {
                Stock.STOCK_ID = DAO.GetGUID();
                DAO.T_STOCKs.InsertOnSubmit(Stock);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int UpdateStock(T_STOCK Stock)
        {
            try
            {
                T_STOCK RawStock = DAO.T_STOCKs.SingleOrDefault(t=>t.STOCK_ID.ToString()==Stock.STOCK_ID.ToString());
                if (RawStock == null) return -1;
                RawStock.STOCK_NAME=Stock.STOCK_NAME;                
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int DeleteStock(T_STOCK Stock)
        {
            try
            {
                T_STOCK RawStock = DAO.T_STOCKs.SingleOrDefault(t => t.STOCK_ID.ToString() == Stock.STOCK_ID.ToString());
                if (RawStock == null) return -1;
                DAO.T_STOCKs.DeleteOnSubmit(RawStock);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int DeleteStock(string StockID)
        {
            try
            {
                T_STOCK RawStock = DAO.T_STOCKs.SingleOrDefault(t => t.STOCK_ID.ToString() == StockID);
                if (RawStock == null) return -1;
                DAO.T_STOCKs.DeleteOnSubmit(RawStock);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public IEnumerable<T_STOCK_AREA> GetStockAreaList(string StockID)
        {
            return DAO.T_STOCK_AREAs.Where(t => t.STOCK_ID.ToString() == StockID).OrderBy(t=>t.AREA_NAME).ToList();
        }

        public IEnumerable<T_STOCK_AREA> GetStockAreaList()
        {
            return DAO.T_STOCK_AREAs.OrderBy(t=>t.AREA_NAME).ToList();
        }

        public IEnumerable<T_FLOOR> GetFloorList(string AreaID)
        {
            return DAO.T_FLOORs.Where(t => t.AREA_ID.ToString() == AreaID).OrderBy(t=>t.FloorName).ToList();
        }

        public IEnumerable<T_FLOOR> GetFloorList()
        {
            return DAO.T_FLOORs.OrderBy(t=>t.FloorName).ToList();
        }

        public T_STOCK_AREA GetStockAreaByID(string ID)
        {
            return DAO.T_STOCK_AREAs.Where(t => t.AREA_ID.ToString() == ID).SingleOrDefault();
        }

        public int AddStockArea(T_STOCK_AREA StockArea)
        {
            try
            {
                StockArea.AREA_ID = DAO.GetGUID();
                //添加区域时默认添加6层
                for (int i = 0; i < 6; i++) {
                    T_FLOOR floor = new T_FLOOR();
                    floor.FloorID = DAO.GetGUID();
                    floor.AREA_ID = StockArea.AREA_ID;
                    floor.FloorName = (i + 1).ToString();
                    DAO.T_FLOORs.InsertOnSubmit(floor);
                }
                DAO.T_STOCK_AREAs.InsertOnSubmit(StockArea);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int UpdateStockArea(T_STOCK_AREA StockArea)
        {
            try
            {
                T_STOCK_AREA RawStockArea = DAO.T_STOCK_AREAs.SingleOrDefault(t => t.AREA_ID.ToString() == StockArea.AREA_ID.ToString());
                if (RawStockArea == null) return -1;
                RawStockArea.AREA_NAME = StockArea.AREA_NAME;
                RawStockArea.T_STOCK = DAO.T_STOCKs.SingleOrDefault(t => t.STOCK_ID.ToString()== StockArea.T_STOCK.STOCK_ID.ToString());
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int DeleteStockArea(T_STOCK_AREA StockArea)
        {
            try
            {
                T_STOCK_AREA RawStockArea = DAO.T_STOCK_AREAs.SingleOrDefault(t => t.AREA_ID.ToString() == StockArea.AREA_ID.ToString());
                if (RawStockArea == null) return -1;
                DAO.T_STOCK_AREAs.DeleteOnSubmit(RawStockArea);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int DeleteStockArea(string AreaID)
        {
            try
            {
                T_STOCK_AREA RawStockArea = DAO.T_STOCK_AREAs.SingleOrDefault(t => t.AREA_ID.ToString() == AreaID);
                if (RawStockArea == null) return -1;
                DAO.T_STOCK_AREAs.DeleteOnSubmit(RawStockArea);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
